package com.shujia.controller;

import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.hadoop.conf.Configuration;

import java.io.IOException;
import java.util.HashMap;

@RestController
public class DalTourController {
    private HTableInterface table;


    {
        //创建hbase连接
        Configuration configuration = new Configuration();
        configuration.set("hbase.zookeeper.quorum", "node1:2181");
        HConnection connection = null;

        try {
            connection = HConnectionManager.createConnection(configuration);
            table = connection.getTable("province_index");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * 通过省编号和时间查询游客数据
     */

    @GetMapping("/getflow")
    public HashMap<String, Long> getFlow(String pid, String time, String field) {


        String rowkey = pid + "_" + time;
        Get get = new Get(rowkey.getBytes());

        get.addColumn("info".getBytes(), field.getBytes());

        Result result = null;
        String s = "";
        try {
            result = table.get(get);
            byte[] value = result.getValue("info".getBytes(), field.getBytes());
            s = Bytes.toString(value);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HashMap<String, Long> map = new HashMap<>();

        map.put("flow", Long.parseLong(s));
        return map;

    }

}
